package com.app.web.controller;

import java.util.List;

import org.aspectj.weaver.ast.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.app.biz.AppCategoryBiz;
import com.app.biz.AppInfoBiz;
import com.app.biz.AppVersionBiz;
import com.app.biz.DataDictionaryBiz;
import com.app.biz.DevUserBiz;
import com.app.entity.AppCategory;
import com.app.entity.AppInfo;
import com.app.entity.AppVersion;
import com.app.entity.DataDictionary;
import com.app.entity.DevUser;

@Controller
public class appcontroller {

	@Autowired
	private AppInfoBiz appInfobiz;

	@Autowired
	private AppCategoryBiz appCategoryBiz;

	@Autowired
	private DataDictionaryBiz dataDictionaryBiz;

	@Autowired
	private AppCategoryBiz appcategoryBiz;

	@Autowired
	private AppVersionBiz appVersionBiz;
	
	@Autowired
	private DevUserBiz devUserBiz;

	@RequestMapping("/index")
	public String index(Model model) {

		return "index";
	}

	@RequestMapping("/information")
	public String information(Model model, Integer cid, Integer page,
			AppInfo appInfo) {
		cid = cid == null ? 0 : cid;
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;


		
		List<AppInfo> list = appInfobiz.getAppInfoPage(0,
				appInfo.getCategoryLevel1(), appInfo.getCategoryLevel2(),
				appInfo.getCategoryLevel3(), appInfo.getSoftwareName(),
				appInfo.getStatus(), appInfo.getFlatformId(), startIndex,
				pageSize);

		List<DataDictionary> dataDictionaries = dataDictionaryBiz.getAll();
		model.addAttribute("dataDictionaries", dataDictionaries);

		int rows = appInfobiz.getRowsCount(cid, appInfo.getCategoryLevel1(),
				appInfo.getCategoryLevel2(), appInfo.getCategoryLevel3(),
				appInfo.getSoftwareName(), appInfo.getStatus(),
				appInfo.getFlatformId());
		int pageCount = (int) Math.ceil((double) rows / pageSize);

		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		model.addAttribute("list", list);
		return "information";
	}

	@RequestMapping(value = "/Level2", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel2(Integer categoryLevel1) {
		System.out.println(categoryLevel1);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel1);
		String result = JSON.toJSONString(list, true);
		return result;
	}

	@RequestMapping(value = "/Level3", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel3(Integer categoryLevel2) {
		System.out.println(categoryLevel2);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel2);
		String result = JSON.toJSONString(list, true);
		return result;
	}

	@RequestMapping("/updateApp")
	public String updateApp(Model model, Integer id) {
		AppInfo appInfo = appInfobiz.getById(id);
		System.out.println(appInfo.getLogoPicPath());
		System.out.println(appInfo.getCategoryLevel1Name());
		model.addAttribute("updateAppInfo", appInfo);
		return "updateApp";
	}

	@RequestMapping("/update")
	public String update(Model model, AppInfo appInfo) {

		System.out.println(appInfo.getLogoPicPath());
		String LogoPicPath = appInfo.getLogoPicPath();
		String path = "statics/uploadfiles/" + LogoPicPath;
		if (path.equals("statics/uploadfiles/")) {
			System.out.println("没有图片路径");
		} else {
			appInfo.setLogoPicPath(path);
			appInfobiz.update(appInfo);
		}

		return "redirect:information";
	}

	@RequestMapping("/Versions")
	public String Versions(Model model, Integer id, String versionNo) {
		if (versionNo.equals("")) {
			System.out.println("这个软件没有版本号");
			return "redirect:information";
		} else {
			System.out.println(id);
			AppInfo appInfo = appInfobiz.getVersion(id);
			System.out.println(appInfo.getFlatformName());
			System.out.println(appInfobiz.getVersion(id));
			model.addAttribute("Version", appInfo);
			List<AppVersion> appVersionList = appVersionBiz
					.getAllAppVersions(57);
			model.addAttribute("appVersionList", appVersionList);
		}
		return "updateVersions";
	}
	
	@RequestMapping("/updateVersions")
	public String updateVersions(Model model,AppVersion appVersion){
		System.out.println(appVersion.getId());

		int row = appVersionBiz.updateVersion(appVersion);
		if(row>0)
			System.out.println("修改成功");
		else
			System.out.println("修改失败");
		
		return "redirect:information";
	}
	
	@RequestMapping("/updateFlatform")
	public String updateFlatform(Model model,AppInfo appInfo){
		
		int result =appInfobiz.updateflatform(appInfo.getStatus(), appInfo.getId());
		if(result>0){
			System.out.println("修改成功");
		}else{
		System.out.println("修改失败");	
		}
		return "redirect:information";
	}
	
}
